/*
 * Boom.java
 *
 * Created on 23. Dezember 2006, 21:52
 *
 * Copyright (c) 2007 Daniel Gronau
 *
 * This file is part of KanjiTori.
 *
 * KanjiTori is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * KanjiTori is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 *
 */

package kanjitori.util;

import com.jme.bounding.BoundingSphere;
import com.jme.image.Texture;
import com.jme.math.FastMath;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.scene.Controller;
import com.jme.scene.Node;
import com.jme.scene.shape.Sphere;
import com.jme.scene.state.AlphaState;
import com.jme.scene.state.TextureState;
import com.jme.scene.state.ZBufferState;
import com.jme.system.DisplaySystem;
import com.jme.util.TextureManager;
import com.jmex.effects.particles.ParticleFactory;
import com.jmex.effects.particles.ParticleGeometry;
import com.jmex.effects.particles.ParticleMesh; 

/**
 *
 * @author Pirx
 */
public class Boom {
    
    static private AlphaState as1;
    static private ZBufferState zstate;
    static private TextureState ts;
    
    static {
        as1 = DisplaySystem.getDisplaySystem().getRenderer().createAlphaState();
        as1.setBlendEnabled(true);
        as1.setSrcFunction(AlphaState.SB_SRC_ALPHA);
        as1.setDstFunction(AlphaState.DB_ONE);
        as1.setTestEnabled(true);
        as1.setTestFunction(AlphaState.TF_GREATER);
        as1.setEnabled(true);
        ts = DisplaySystem.getDisplaySystem().getRenderer().createTextureState();
        ts.setTexture(
                TextureManager.loadTexture("data/flaresmall.jpg",
                Texture.MM_LINEAR_LINEAR,
                Texture.FM_LINEAR));
        ts.setEnabled(true);
        zstate = DisplaySystem.getDisplaySystem().getRenderer().createZBufferState();
        zstate.setEnabled(false);
        zstate.setFunction(ZBufferState.CF_ALWAYS);
    }
    
    /** Static class */
    private Boom() { 
    }
    
    public static void setExplosion(Node parentNode, ColorRGBA startColor, ColorRGBA endColor, 
            float startSize, float endSize, float radius, int particles) {
        
        ParticleMesh pMesh = ParticleFactory.buildParticles("particles", particles);
        pMesh.setEmissionDirection(new Vector3f(0, 1, 0));
        pMesh.setInitialVelocity(.6f);
        pMesh.setStartSize(startSize);
        pMesh.setEndSize(endSize);
        pMesh.setMinimumLifeTime(500f);
        pMesh.setMaximumLifeTime(1000f);
        pMesh.setStartColor(startColor);
        pMesh.setEndColor(endColor);
        pMesh.setMinimumAngle(0f);
        pMesh.setMaximumAngle(FastMath.PI);
        pMesh.getParticleController().setControlFlow(false);
        pMesh.getParticleController().setRepeatType(Controller.RT_CLAMP);
        pMesh.getParticleController().setSpeed(1f);
        pMesh.warmUp(1000);
        pMesh.setCameraFacing(true);
        pMesh.setRenderState(zstate);

        Sphere sphere = new Sphere("sphere", 10, 10, radius);
        sphere.setCullMode(Sphere.CULL_ALWAYS);
        
        
        Node node = new Node("explosion");
        node.setRenderState(ts);
        node.setRenderState(as1);
        node.attachChild(pMesh);
        node.attachChild(sphere);
        pMesh.setOriginOffset(parentNode.getLocalTranslation()); 

        sphere.setModelBound(new BoundingSphere());
        sphere.updateModelBound();
        
        pMesh.setGeomBatch(sphere.getBatch(0));
        pMesh.setModelBound(new BoundingSphere());
        pMesh.updateModelBound();
        
        parentNode.attachChild(node);
        parentNode.updateRenderState();
        parentNode.updateWorldBound();
        parentNode.updateWorldVectors();
    }
    
    public static void explode(Node parentNode) {
       ((ParticleGeometry) ((Node)parentNode.getChild("explosion")).getChild(0)).forceRespawn();
    }
    
}
